package com.cat.binaryTree;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-largest-value-in-each-tree-row/description/?envType=problem-list-v2&envId=binary-tree
 * @create 2025/9/21 20:39
 * @since JDK17
 */

public class Solution57 {
    public List<Integer> largestValues(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        Deque<TreeNode> q = new ArrayDeque<>();
        List<Integer> ans = new ArrayList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int max = Integer.MIN_VALUE;
            for (int i = 0, end = q.size(); i < end; i++) {
                var x = q.poll();
                max = Math.max(max, x.val);
                if (x.left != null) {
                    q.offer(x.left);
                }
                if (x.right != null) {
                    q.offer(x.right);
                }
            }
            ans.add(max);
        }

        return ans;
    }
}
